package kr.hkit.androidcomplete02.network;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import kr.hkit.androidcomplete02.R;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

public class Dom_parserActivity extends Activity {
	private ListView list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network_xml);
		list = (ListView) findViewById(R.id.listview);
		new ParsingXML().execute("order.xml");
	}
	
	private class ParsingXML 
			extends AsyncTask<String, Void, ArrayList<Item>>{
		private ProgressDialog progressDlg;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDlg = ProgressDialog.show(Dom_parserActivity.this, "Wait", "Xml Parsing....");
		}
		
		@Override
		protected void onPostExecute(ArrayList<Item> result) {
			super.onPostExecute(result);
			MyAdapter adapter = new MyAdapter(Dom_parserActivity.this, result);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}
		
		@Override
		protected ArrayList<Item> doInBackground(String... params) {
			StringBuilder sb = new StringBuilder();
			String str = null;
			try {
				BufferedReader br = new BufferedReader(
						new InputStreamReader(openFileInput(params[0])));
				while((str = br.readLine()) != null){
					sb.append(str);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return parsingXml(sb.toString());
		}

		private ArrayList<Item> parsingXml(String xml) {
			ArrayList<Item> arItem = new ArrayList<Item>();
			Item itemClass;
			
			try {
				InputStream iStream = new ByteArrayInputStream(xml.getBytes("utf-8"));
				
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document dc = builder.parse(iStream);
				
				Element order = dc.getDocumentElement();
				NodeList items = order.getElementsByTagName("item");
				
				for(int i=0; i<items.getLength(); i++){
					itemClass = new Item();
					Node itemNode = items.item(i);
					Node itemValue = itemNode.getFirstChild();
					itemClass.setItemName(itemValue.getNodeValue());
					
					if(itemValue.hasAttributes()){
						NamedNodeMap attrs = itemNode.getAttributes();
						for(int j=0; j<attrs.getLength(); j++){
							Node attr = attrs.item(j);
							if(attr.getNodeName().equalsIgnoreCase("price")){
								itemClass.setItemPrice(Integer.parseInt(attr.getNodeValue()));
							}else{
								itemClass.setMakerName(attr.getNodeValue());
							}
						}
					}
					arItem.add(itemClass);
				}
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();	
			}
			return arItem;
		}
	}
}
